import axios from 'axios'
import pinia from '@/stores/storeInit'
import { useUserStore } from '@/stores/user'
import {judgeDataIsNotNull} from '@/tools/normal'
import { ElMessage  } from 'element-plus'

export const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    // baseURL: "http://localhost:8080",
    baseURL:"124.220.64.235:8080",
    // 超时
    timeout: 10000
})

service.interceptors.request.use(
    (config)=>{
        const userStore = useUserStore();
        if(judgeDataIsNotNull(userStore.token)){
            config.headers.Authorization = `${userStore.token}`;
        }
        return config;
    }
)

service.interceptors.response.use(res=>{
    // let status_code=res.status;
    // let status_data=res.status;  //{data:"",stateMessage:""}
   return res  
},error=>{
    let {message} = error;
    const {response} = error;
    if(message == "Network Error") {
        message="服务器异常"
    }else if(message.includes("timeout")){
        message="系统接口请求超时"
    }else if(judgeDataIsNotNull(response)){
        message=response.data.detail||"接口异常";
    }
    ElMessage({
        message: message,
        type: 'error',
        duration: 5 * 1000
      })

    return Promise.reject(error)
})